package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.enums.PaymentTypeEnum;
import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.order.mapper.RefundInfoMapper;
import com.atguigu.yygh.order.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/9/10 0:55
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        // 一条支付记录只能有一条退款记录 -- 当用户多开窗口点击取消预约点击多次的时候需要做判断
        Long orderId = paymentInfo.getOrderId();
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        RefundInfo selectRefundInfo = baseMapper.selectOne(queryWrapper);
        if(selectRefundInfo != null) {
            return selectRefundInfo;
        }
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(PaymentTypeEnum.WEIXIN.getStatus());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setSubject("退款中...");
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        baseMapper.insert(refundInfo); // 添加到退款信息表
        return refundInfo;
    }
}
